package com.elong.businesstravel.base.baidusdk.map;

import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationOverlay;
import com.baidu.mapapi.map.MyLocationOverlay.LocationMode;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;

/**
 * 百度mapview
 * 
 * @author jiahongfei
 * 
 */
public class BaiduMapView {

	public interface LocationOverlayDispatchTap {
		public boolean dispatchTap();
	}

	private Context mContext;
	private LocationOverlayDispatchTap mLocationOverlayDispatchTap;
	private MapView mMapView;
	private MapController mMapController;
	private LocationOverlay myLocationOverlay = null;

	public BaiduMapView(Context context, MapView mapView) {
		mContext = context;
		mMapView = mapView;

		initBaiduMapView();

		initLocationOverlay();
	}

	private void initBaiduMapView() {
		// 地图初始化
		mMapController = mMapView.getController();
		mMapView.getController().setZoom(16);
		mMapView.getController().enableClick(true);
		mMapView.setBuiltInZoomControls(true);
	}

	private void initLocationOverlay() {
		// 定位图层初始化
		myLocationOverlay = new LocationOverlay(mMapView);
		// 添加定位图层
		myLocationOverlay.enableCompass();
		mMapView.getOverlays().add(myLocationOverlay);

	}

	/**
	 * 设置定位数据
	 * 
	 * @param locData
	 */
	public void setLocationOverlayData(LocationData locData) {
		myLocationOverlay.setData(locData);
	}
	
	public void setLocationMode(LocationMode locationMode){
		myLocationOverlay.setLocationMode(locationMode);
	}
	
	/**
	 * 修改位置图标
	 * 
	 * @param marker
	 */
	public void modifyLocationOverlayIcon(Drawable marker) {
		// 当传入marker为null时，使用默认图标绘制
		myLocationOverlay.setMarker(marker);
		// 修改图层，需要刷新MapView生效
		mMapView.refresh();
	}

	/**
	 * 设置定位图层点击
	 * 
	 * @param locationOverlayDispatchTap
	 */
	public void setLocationOverlayDispatchTap(
			LocationOverlayDispatchTap locationOverlayDispatchTap) {
		mLocationOverlayDispatchTap = locationOverlayDispatchTap;

	}
	
	/**
	 * 返回定位图层
	 * @return
	 */
	public LocationOverlay getLocationOverlay(){
		return myLocationOverlay;
	}

	// 继承MyLocationOverlay重写dispatchTap实现点击处理
	public class LocationOverlay extends MyLocationOverlay {

		public LocationOverlay(MapView mapView) {
			super(mapView);
		}

		@Override
		protected boolean dispatchTap() {
			// 处理点击事件,弹出泡泡
			if (null != mLocationOverlayDispatchTap) {
				return mLocationOverlayDispatchTap.dispatchTap();
			}
			return true;
		}

	}

}
